﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyData.ExpressionBuilder.Model;
using MyData.ExpressionBuilder;
using MyData.ExpressionBuilder.BaseExtensions;

namespace MyData.Extensions
{
  [ExpressionBuilderMetadata(Name = "StringExpressionTypeConverter", Description = "", ImagePath = "")]
  public class StringExpressionTypeConverter : ExpressionTypeConverterBase
  {
    protected override object CommitedTextExtension(List<ListItem> nameValues)
    {

      object value = nameValues.Count > 0 ? nameValues[0].Value : string.Empty;

      return value.ToString();
    }
  }

  [ExpressionBuilderMetadata(Name = "QuotedValueExpressionTypeConverter", Description = "", ImagePath = "")]
  public class QuotedValueExpressionTypeConverter : ExpressionTypeConverterBase
  {
    protected override object CommitedTextExtension(List<ListItem> nameValues)
    {

      object value = nameValues.Count > 0 ? nameValues[0].Value : string.Empty;

      return value.ToString().QuotedValue();
    }
  }

  [ExpressionBuilderMetadata(Name = "DictionaryTypeConverter", Description = "", ImagePath = "")]
  public class DictionaryTypeConverter : ExpressionTypeConverterBase
  {
    protected override object CommitedTextExtension(List<ListItem> nameValues)
    {
      string name = string.Empty;
      string value = string.Empty;
      string convertedValue = string.Empty;

      if (nameValues.Count > 0)
      {
        foreach (ListItem item in nameValues)
        {
          name = item.Name;
          value = item.Value == null ? string.Empty : item.Value.ToString();
          convertedValue += "{" + name.QuotedValue() + ", " + value + "},";
        }
      }

      //Remove ending comma
      if (convertedValue.Length > 0)
        convertedValue = convertedValue.Substring(0, convertedValue.Length - 1);

      return convertedValue;
    }
  }


  //New System.Collections.Generic.List(Of String) From{{"Title"}}
  [ExpressionBuilderMetadata(Name = "ListTypeConverter", Description = "", ImagePath = "")]
  public class ListTypeConverter : ExpressionTypeConverterBase
  {
    protected override object CommitedTextExtension(List<ListItem> nameValues)
    {
      string name = string.Empty;
      string value = string.Empty;
      string convertedValue = string.Empty;

      if (nameValues.Count > 0)
      {
        foreach (ListItem item in nameValues)
        {
          name = item.Name;
          value = item.Value == null ? string.Empty : item.Value.ToString();
          convertedValue += "{" + name.QuotedValue() + "},";
        }
      }

      //Remove ending comma
      if (convertedValue.Length > 0)
        convertedValue = convertedValue.Substring(0, convertedValue.Length - 1);

      return "New System.Collections.Generic.List(Of String) From{" + convertedValue + "}";
    }
  }

  [ExpressionBuilderMetadata(Name = "SharePointExpressionTypeConverter", Description = "", ImagePath = "")]
  public class SharePointExpressionTypeConverter : ExpressionTypeConverterBase
  {
    protected override object CommitedTextExtension(List<ListItem> nameValues)
    {
      string value = new DictionaryTypeConverter().CommitedText(nameValues).ToString();

      return "New System.Collections.Generic.Dictionary(Of String, Object) From{" + value + "}";
    }
  }

  ////[ExpressionBuilderMetadata(Name = "ListTypeConverter", Description = "", ImagePath = "")]
  ////public class ListTypeConverter : ExpressionTypeConverterBase
  ////{
  ////  protected override object CommitedTextExtension(List<ListItem> nameValues)
  ////  {
  ////    string name = string.Empty;
  ////    string value = string.Empty;
  ////    string convertedValue = string.Empty;

  ////    if (nameValues.Count > 0)
  ////    {
  ////      foreach (ListItem item in nameValues)
  ////      {
  ////        name = item.Name;
  ////        convertedValue += "{" + name.QuotedValue() + "},";
  ////      }
  ////    }

  ////    //Remove ending comma
  ////    if (convertedValue.Length > 0)
  ////      convertedValue = convertedValue.Substring(0, convertedValue.Length - 1);

  ////    return convertedValue;
  ////  }
  ////}

  ////[ExpressionBuilderMetadata(Name = "ListExpressionTypeConverter", Description = "", ImagePath = "")]
  ////public class ListExpressionTypeConverter : ExpressionTypeConverterBase
  ////{
  ////  protected override object CommitedTextExtension(List<ListItem> nameValues)
  ////  {
  ////    string value = new ListTypeConverter().CommitedText(nameValues).ToString();

  ////    return "New System.Collections.Generic.List(Of String) From{" + value + "}";
  ////  }
  ////}

}
